<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #inp {
            width: 600px;
            height: 300px;
            margin: 20px auto;
            border-top: none;
            border: 1px solid #ccc;
        }

        input {
            width: 80%;
            height: 30px;
            border: none;
            border-bottom: 1px solid #ccc;
        }

        button {
            width: 20%;
            height: 32px;
            outline: none;
            border: none;
            background-color: #c0c0c0;
        }

        li {
            list-style: none;
            margin-left: 10px;
        }
        .active{
            background-color: antiquewhite;
        }
    </style>
</head>

<body>
    <div id="box">
        <div id="inp">
            <input type="text" name="" placeholder="输入" @keydown.down="down()"  @keyup.up="up()" v-model="msg" id=""><button @click="change()">百度一下</button>
            <ul>
                <li v-for="(item,index) in arr" :class="{active:index==current}">{{item}}</li>
            </ul>
        </div>


    </div>
</body>
<script src="./js/vue.js"></script>
<script src="./js/vue-resource.js"></script>
<script>
    let vm = new Vue({
        el: '#box',
        data: {
            arr: [],
            msg: '',
            current:0, //下标
        },
        methods: {
            change() {
                console.log(this.msg);
                // wd=iphone&jsonp=cb
                if (!this.msg == "") {
                    let url = "https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?"
                this.$http.jsonp(url, {
                    params: { wd: this.msg },
                    jsonp: 'cb',
                }).then(res => {
                    this.arr = res.body.s
                    console.log(res.body.s);
                })
                }
            },
            down(){
                this.current++
                if(this.current > this.arr.length-1){
                    this.current = 0
                }
            },
            up(){
                this.current--
                if(this.current<0){
                    this.current=this.arr.length-1
                }
            }
        },
        
    })
</script>

</html>